package com.bstek.dorado.sample.datatypeext;

import com.bstek.dorado.core.Context;
import com.bstek.dorado.idesupport.RuleSetBuilder;
import com.bstek.dorado.idesupport.RuleTemplateBuilder;
import com.bstek.dorado.idesupport.RuleTemplateManager;
import com.bstek.dorado.idesupport.model.Rule;
import com.bstek.dorado.idesupport.model.RuleSet;

public class DoradoRuleSet {
	// RuleSet
	private RuleSet ruleSet;

	private RuleSet getRuleSet() {
		try {
			if (ruleSet == null)
				ruleSet = this.buildRuleSet();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return ruleSet;
	}

	private RuleSet buildRuleSet() throws Exception {
		Context context = Context.getCurrent();
		RuleTemplateBuilder ruleTemplateBuilder = (RuleTemplateBuilder) context
				.getServiceBean("idesupport.ruleTemplateBuilder");
		RuleSetBuilder ruleSetBuilder = (RuleSetBuilder) context
				.getServiceBean("idesupport.ruleSetBuilder");

		RuleTemplateManager ruleTemplateManager = ruleTemplateBuilder
				.getRuleTemplateManager();
		RuleSet ruleSet = ruleSetBuilder.buildRuleSet(ruleTemplateManager);
		return ruleSet;
	}

	public Rule getRule(String name) {
		RuleSet ruleSet = getRuleSet();
		return ruleSet.getRule(name);
	}
}
